package com.lu.common.controller;

import java.util.List;
import java.util.Map;

import com.lu.common.utils.PageUtils;
import com.lu.common.utils.Query;
import com.lu.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lu.common.domain.KnowledgeUserDO;
import com.lu.common.service.KnowledgeUserService;

/**
 * 用户
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-08-09 23:24:23
 */
 
@Controller
@RequestMapping("/common/knowledgeUser")
public class KnowledgeUserController {
	@Autowired
	private KnowledgeUserService knowledgeUserService;

   /**
    *  根据id查询
	*  @return
	*/
    @ResponseBody
    @GetMapping("/get")
    public R get(Long id){
        KnowledgeUserDO knowledgeUser = knowledgeUserService.get(id);
        R r = new R();
        r.put("data",knowledgeUser);
        return r;
    }

	/**
	 * 查询列表数据
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	public PageUtils getKnowledgeUserList(@RequestParam Map<String, Object> params){
        Query query = new Query(params);
		List<KnowledgeUserDO> knowledgeUserList = knowledgeUserService.list(query);
		int total = knowledgeUserService.count(query);
		PageUtils pageUtils = new PageUtils(knowledgeUserList, total);
		return pageUtils;
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( KnowledgeUserDO knowledgeUser){
		if(knowledgeUserService.save(knowledgeUser)>0){
			return R.ok();
		}
		return R.error();
	}

    /**
     * 批量保存
     */
    @ResponseBody
    @PostMapping("/saveBatch")
    public R saveBatch( List<KnowledgeUserDO> knowledgeUser){
        try {
			knowledgeUserService.saveBatch(knowledgeUser);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( KnowledgeUserDO knowledgeUser){
		knowledgeUserService.update(knowledgeUser);
		return R.ok();
	}

    /**
     * 批量修改
     */
    @ResponseBody
    @PostMapping("/updateBatch")
    public R updateBatch( List<KnowledgeUserDO> knowledgeUser){
        try {
			knowledgeUserService.updateBatch(knowledgeUser);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	public R remove( Long id){
		if(knowledgeUserService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		knowledgeUserService.batchRemove(ids);
		return R.ok();
	}
	
}
